package com.by.controller;


import cn.hutool.core.bean.BeanUtil;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.pojo.dto.AdminPageQueryDTO;
import com.by.pojo.entity.Admin;
import com.by.pojo.entity.Dept;
import com.by.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "管理员接口")
@RequestMapping("/api")
@RestController
public class AdminController extends BaseController {

    @Autowired
    private AdminService adminService;

    public AdminController(AdminService adminService) {
        this.adminService = adminService;
    }


    @ApiOperation("保存管理员")
    @PostMapping("/admin")
    public ResultBean save(@RequestBody Admin admin) {
        admin.setDisable(0);
        System.out.println(admin);
        int rs = adminService.save(admin);
        return saveResult(rs);
    }


    @ApiOperation("查询员工列表")
    @PostMapping("/admin/list")
    public ResultBean<PageBean<List<Admin>>> query(@RequestBody AdminPageQueryDTO adminPageQueryDTO) {
        PageBean bean = adminService.query(adminPageQueryDTO);
        return success("员工列表", bean);
    }


    @ApiImplicitParam(name = "id", value = "员工id", dataTypeClass = Integer.class, paramType = "query", required = true)
    @ApiOperation("删除员工")
    @DeleteMapping("/admin")
    public ResultBean delete(@RequestParam(required = true) Integer id) {
        int rs = adminService.delete(id);
        return deleteResult(rs);
    }


    @ApiOperation("修改员工信息")
    @PutMapping("/admin")
    public ResultBean update(@RequestBody Admin admin){
        int rs = adminService.update(admin);
        return updateResult(rs);
    }

    @ApiOperation("根据id查询员工信息")
    @GetMapping("/admin/id")
    public ResultBean<Admin> selectById(Integer id) {
        System.out.println("员工id" + id);
        return success("员工信息", adminService.selectById(id));
    }

}
